package online.sh.cn.fy.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.SqlUtil;

/**
 * Servlet implementation class fyhelpAPIForWX
 */
@WebServlet("/fyhelpAPIForWX")
public class fyhelpAPIForWX extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public fyhelpAPIForWX() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取输入句子
		String search = URLDecoder.decode(request.getParameter("search"),"UTF-8");
		
    	List<String> list = null;
    	JSONArray json = null;

		SqlUtil sqlutil= new SqlUtil();
		//HttpSession session = request.getSession();

		
		//调取所有关键字
		List<Map> keywords=sqlutil.getfyHelp();
		
		Map<String,String> res = matchKeyWord(search,keywords);
		if(res != null) {
			System.out.println(res.get("help"));
			System.out.println(res.get("number"));
			//获取下级
			List<Map> answers = sqlutil.getfyhelpKeys(res.get("number").toString());
			//session.setAttribute("keywords", res.get("number").toString());

//			list = new ArrayList<String>();
//			for(Map answer:answers){
//				list.add(answer.get("help").toString());
//			}
			json = new JSONArray();
			for(Map answer:answers){
	            JSONObject jo = new JSONObject();
	            System.out.println(answer.get("help").toString());
	            
	          //切分关键字
				String[] tmp = answer.get("help").toString().split("http");
	            
	            jo.put("help", URLEncoder.encode(tmp[0].toString()));
	            if(tmp.length > 1){
		            jo.put("helpurl", URLEncoder.encode("http" + tmp[1].toString()));
	            }else{
		            jo.put("helpurl", URLEncoder.encode("#"));
	            }
	            jo.put("number", answer.get("number").toString());
	            jo.put("level", answer.get("level").toString());

	             
	            json.add(jo);
	        }
			
			
			//System.out.println(answers.get(0).get("help"));
		}
    	//request.setAttribute("list", list);
		String rtn = "";
		if(null != json){
			System.out.println(json.toString());
			rtn =  json.toString();
		}

    	
		ResponseUtils.renderJson(response, rtn);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 	}

	//匹配关键字与搜索句子
	public static Map matchKeyWord(String search,List<Map> keywords){
		Map<String,String> res=null;
		
		
		for(Map key:keywords){
			//切分关键字
			String[] tmp = key.get("help").toString().split("/");
			for(String temp:tmp){
				if(search.toUpperCase().indexOf(temp) != -1){
					return key;
				}
			}
		}
		
		return res;
		
	}
}
